package controller

import (
	"github.com/cy77cc/hioshop/models"
	"github.com/cy77cc/hioshop/utils"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
)

type OrderHandler struct{}

type orderListResponse struct {
	models.Order
	GoodsList       []models.OrderGoods `json:"goodsList"`
	GoodsCount      int                 `json:"goodsCount"`
	AddTime         string              `json:"add_time"`
	OrderStatusText string              `json:"order_status_text"`
	HandleOption    map[string]bool     `json:"handle_option"`
}

func (OrderHandler) OrderCount(ctx *gin.Context) {
	res := utils.NewResponse()
	id, exits := ctx.Get("id")
	if !exits {
		res.SetRes(1, "id不存在", nil)
		ctx.JSON(http.StatusBadRequest, res)
		return
	}

	userId := id.(uint)
	order := models.Order{}
	order.UserId = userId

	toPay, err := order.GetOrderCountByStatus([]int{101, 801})
	if err != nil {
		res.SetRes(1, "查询失败", err)
		ctx.JSON(http.StatusOK, res)
		return
	}
	toDelivery, err := order.GetOrderCountByStatus([]int{300})
	if err != nil {
		res.SetRes(1, "查询失败", err)
		ctx.JSON(http.StatusOK, res)
		return
	}
	toReceive, err := order.GetOrderCountByStatus([]int{301})
	if err != nil {
		res.SetRes(1, "查询失败", err)
		ctx.JSON(http.StatusOK, res)
		return
	}

	res.SetRes(0, "success", map[string]any{
		"toPay":      toPay,
		"toDelivery": toDelivery,
		"toReceive":  toReceive,
	})

	ctx.JSON(http.StatusOK, res)
}

func (OrderHandler) List(ctx *gin.Context) {
	res := utils.NewResponse()
	id, exits := ctx.Get("id")
	if !exits {
		res.SetRes(1, "id不存在", nil)
		ctx.JSON(http.StatusBadRequest, res)
		return
	}

	userId := id.(uint)
	showType := ctx.Query("showType")
	pageStr := ctx.Query("page")
	sizeStr := ctx.Query("size")

	page, _ := strconv.Atoi(pageStr)
	pageSize, _ := strconv.Atoi(sizeStr)

	var status []int
	switch showType {
	case "0":
		status = []int{101, 102, 103, 201, 202, 203, 300, 301, 302, 303, 401}
	case "1":
		status = []int{101} // 待付款订单
	case "2":
		status = []int{300} // 待发货订单
	case "3":
		status = []int{301} // 待收货订单
	case "4":
		status = []int{302, 303}
	}

	order := models.Order{}
	orderCount, err := order.GetOrderCountByStatus(status)
	if err != nil {
		res.SetRes(1, "error", err)
		ctx.JSON(http.StatusBadRequest, res)
		return
	}
	orderList, err := order.GetOrderListByStatus(status, page, pageSize)
	if err != nil {
		res.SetRes(1, "error", err)
		ctx.JSON(http.StatusBadRequest, res)
		return
	}

	newOrderList := make([]orderListResponse, len(orderList))
	for k, v := range orderList {
		orderGoods := models.OrderGoods{}
		orderGoods.UserId = uint(userId)
		orderGoods.OrderId = v.ID
		newOrderList[k].GoodsList, err = orderGoods.GetGoodsList()
		if err != nil {
			res.SetRes(1, "error", err)
			ctx.JSON(http.StatusBadRequest, res)
			return
		}
		for i := range newOrderList[k].GoodsList {
			newOrderList[k].GoodsCount += int(newOrderList[k].GoodsList[i].Number)
		}

		newOrderList[k].AddTime = time.Unix(int64(v.AddTime), 0).Format("2006-01-02 15:04:05")
		newOrderList[k].OrderStatusText = v.GetOrderStatusText()
		newOrderList[k].HandleOption = v.GetOrderHandleOption()
	}

	res.SetRes(0, "success", map[string]any{
		"data":  newOrderList,
		"count": orderCount,
	})
	ctx.JSON(http.StatusOK, res)
}
